This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Antes de comenzar, cargamos nuestras librerias:
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(readxl)
library(xts)
library(TSstudio)
library(dygraphs)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
## ✔ tibble 3.1.8 ✔ tsibble 1.1.2
## ✔ dplyr 1.0.9 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.2.0 ✔ feasts 0.3.0
## ✔ ggplot2 3.4.0 ✔ fable 0.3.2
## Warning: package 'ggplot2' was built under R version 4.2.2
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ tsibble::index() masks zoo::index()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
library(astsa)
##
## Attaching package: 'astsa'
## The following object is masked from 'package:forecast':
##
## gas
library(timetk)
library(TSA)
## Registered S3 methods overwritten by 'TSA':
## method from
## fitted.Arima forecast
## plot.Arima forecast
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
library(lmtest)
library(dplyr)
library(parsnip)
##
## Attaching package: 'parsnip'
## The following object is masked from 'package:fabletools':
##
## null_model
library(rsample)
library(timetk)
library(modeltime)
##
## Attaching package: 'modeltime'
## The following object is masked from 'package:TSA':
##
## season
## The following object is masked from 'package:astsa':
##
## trend
library(tsibble)
library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.0.0 ──
## ✔ broom 1.0.0 ✔ recipes 1.0.1
## ✔ dials 1.0.0 ✔ tune 1.0.1
## ✔ infer 1.0.3 ✔ workflows 1.1.0
## ✔ modeldata 1.0.1 ✔ workflowsets 1.0.0
## ✔ purrr 0.3.4 ✔ yardstick 1.1.0
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ yardstick::accuracy() masks fabletools::accuracy(), forecast::accuracy()
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ infer::generate() masks fabletools::generate()
## ✖ infer::hypothesize() masks fabletools::hypothesize()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ✖ parsnip::null_model() masks fabletools::null_model()
## ✖ yardstick::spec() masks TSA::spec()
## ✖ recipes::step() masks stats::step()
## • Learn how to get started at https://www.tidymodels.org/start/
library(forecast)
library(greybox)
## Registered S3 method overwritten by 'greybox':
## method from
## print.pcor lava
## Package "greybox", v1.0.6 loaded.
##
## Attaching package: 'greybox'
## The following objects are masked from 'package:fabletools':
##
## forecast, MAE, MAPE, MASE, ME, MPE, MSE, RMSSE
## The following object is masked from 'package:tsibble':
##
## measures
## The following object is masked from 'package:tidyr':
##
## spread
## The following object is masked from 'package:lubridate':
##
## hm
Luego, abrimos nuestra base de datos:
base2<-read_excel("oro.xlsx", col_names = TRUE)
## New names:
## • `` -> `...5`
venta<-base2$Venta
tsventa<-ts(venta,start = c(2017,1,1), frequency = c(260))
ts.plot(tsventa, gpars = list(col = c("black", "red")),main="Serie precio venta oro en Colombia")
La serie utilizada corresponde al valor monetario del oro en kilogramo
en Colombia, que tiene un precio diario. A fin de este ejercicio, se
seleccionó los datos desde 2017-01-01 a 2019-12-31.
Para empezar vamos a ver, mediante Box Cox, si existe varianza marginal.
forecast::BoxCox.lambda(tsventa, method = "guerrero", lower = -3, upper = 3)
## [1] -2.999947
Concorde a que el lambda es distinto de 1, realizaremos la transformación vÃa logaritmo.
loro<-log(tsventa)
plot(loro, main="Serie transformada venta de oro en Colombia")
Aunque no se observan cambios notables en la gráfica, se procederá a
trabajar con la serie transformada. Ahora bien, vamos a ver si es
necesario realizar una diferenciación.
ar(loro)
##
## Call:
## ar(x = loro)
##
## Coefficients:
## 1 2 3
## 0.9970 -0.0836 0.0811
##
## Order selected 3 sigma^2 estimated as 0.000155
tseries::adf.test(loro,k=3)
##
## Augmented Dickey-Fuller Test
##
## data: loro
## Dickey-Fuller = -1.4616, Lag order = 3, p-value = 0.8062
## alternative hypothesis: stationary
Como el test de Dickey-Fuller no rechaza la hipótesis nula, concluÃmos que sà debemos diferenciar. Dado que tenemos tres coeficientes, nuestra diferenciación queda dada por:
dloro<-diff(loro,lag=1)
plot(dloro, main="Serie venta de oro en Colombia diferenciada")
Al examinar si debemos realiar otra diferenciación, notamos que no es necesario dado que el test de Dickey-Fuller rechaza la hipótesis nula.
ar(dloro)
##
## Call:
## ar(x = dloro)
##
## Coefficients:
## 1 2
## -0.0286 -0.0755
##
## Order selected 2 sigma^2 estimated as 6.841e-05
tseries::adf.test(dloro,k = 2)
## Warning in tseries::adf.test(dloro, k = 2): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: dloro
## Dickey-Fuller = -16.89, Lag order = 2, p-value = 0.01
## alternative hypothesis: stationary
Concorde a la información, vamos a ver cómo es el comportamiento de la serie descompuesta:
lorodes<-decompose(loro)
plot(lorodes)
Luego, observamos los gráficos ACF y PACF
acf(dloro)
pacf(dloro)
Al ver la dispersión tenemos que,
astsa::lag1.plot(dloro,12)
mvspec(dloro)
abline(v=114.67,col="red")
dloro_tb<-as_tsibble(dloro,index=tibble(fecha))
dloro_tb
## # A tsibble: 781 x 2 [1s] <UTC>
## index value
## <dttm> <dbl>
## 1 2017-01-02 09:41:27 -0.00906
## 2 2017-01-03 19:22:55 0.00834
## 3 2017-01-05 05:04:22 0.000833
## 4 2017-01-06 14:46:21 -0.00401
## 5 2017-01-08 00:27:49 0
## 6 2017-01-09 10:09:16 -0.00313
## 7 2017-01-10 19:50:44 0.0141
## 8 2017-01-12 05:32:11 0.0265
## 9 2017-01-13 15:13:39 -0.0257
## 10 2017-01-15 00:55:38 0.00730
## # … with 771 more rows
## # ℹ Use `print(n = ...)` to see more rows
colnames(dloro_tb)<-c("Fecha","Dato")
dloro_tb$dia <- wday(dloro_tb$Fecha, label = TRUE, abbr = TRUE,week_start = 1)
dloro_tb$mes <- factor(month.abb[month(dloro_tb$Fecha)], levels = month.abb)
dloro_tb%>%mutate(diff_ND=Dato-lag(Dato))%>%plot_seasonal_diagnostics(.date_var = Fecha,.value = diff_ND,.feature_set = c("wday.lbl"),.geom="boxplot")
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
## Warning: The following aesthetics were dropped during statistical transformation:
## y_plotlyDomain
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
dloro_tb%>%mutate(diff_ND=Dato-lag(Dato))%>%plot_seasonal_diagnostics(.date_var = Fecha,.value = diff_ND,.feature_set = c("month.lbl"),.geom="boxplot")
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
## Warning: The following aesthetics were dropped during statistical transformation:
## y_plotlyDomain
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
ajusteARIMA<-forecast::Arima(tsventa,order=c(2,1,0),lambda=0,include.constant=TRUE,fixed=list(0,NA,NA))
coeftest(ajusteARIMA)
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar2 -0.07549086 0.03586447 -2.1049 0.03530 *
## drift 0.00045915 0.00027714 1.6568 0.09757 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ajusteARIMA2<-forecast::Arima(tsventa,order=c(0,1,2),lambda=0,include.constant=TRUE,fixed=list(0,NA,NA))
coeftest(ajusteARIMA2)
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ma2 -0.08303306 0.03746879 -2.2161 0.02669 *
## drift 0.00045827 0.00027331 1.6767 0.09360 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
h=3
lserie=length(loro)
ntrain=trunc(length(loro)*0.8)
ntrain
## [1] 625
time(loro)
## Time Series:
## Start = c(2017, 1)
## End = c(2020, 2)
## Frequency = 260
## [1] 2017.000 2017.004 2017.008 2017.012 2017.015 2017.019 2017.023 2017.027
## [9] 2017.031 2017.035 2017.038 2017.042 2017.046 2017.050 2017.054 2017.058
## [17] 2017.062 2017.065 2017.069 2017.073 2017.077 2017.081 2017.085 2017.088
## [25] 2017.092 2017.096 2017.100 2017.104 2017.108 2017.112 2017.115 2017.119
## [33] 2017.123 2017.127 2017.131 2017.135 2017.138 2017.142 2017.146 2017.150
## [41] 2017.154 2017.158 2017.162 2017.165 2017.169 2017.173 2017.177 2017.181
## [49] 2017.185 2017.188 2017.192 2017.196 2017.200 2017.204 2017.208 2017.212
## [57] 2017.215 2017.219 2017.223 2017.227 2017.231 2017.235 2017.238 2017.242
## [65] 2017.246 2017.250 2017.254 2017.258 2017.262 2017.265 2017.269 2017.273
## [73] 2017.277 2017.281 2017.285 2017.288 2017.292 2017.296 2017.300 2017.304
## [81] 2017.308 2017.312 2017.315 2017.319 2017.323 2017.327 2017.331 2017.335
## [89] 2017.338 2017.342 2017.346 2017.350 2017.354 2017.358 2017.362 2017.365
## [97] 2017.369 2017.373 2017.377 2017.381 2017.385 2017.388 2017.392 2017.396
## [105] 2017.400 2017.404 2017.408 2017.412 2017.415 2017.419 2017.423 2017.427
## [113] 2017.431 2017.435 2017.438 2017.442 2017.446 2017.450 2017.454 2017.458
## [121] 2017.462 2017.465 2017.469 2017.473 2017.477 2017.481 2017.485 2017.488
## [129] 2017.492 2017.496 2017.500 2017.504 2017.508 2017.512 2017.515 2017.519
## [137] 2017.523 2017.527 2017.531 2017.535 2017.538 2017.542 2017.546 2017.550
## [145] 2017.554 2017.558 2017.562 2017.565 2017.569 2017.573 2017.577 2017.581
## [153] 2017.585 2017.588 2017.592 2017.596 2017.600 2017.604 2017.608 2017.612
## [161] 2017.615 2017.619 2017.623 2017.627 2017.631 2017.635 2017.638 2017.642
## [169] 2017.646 2017.650 2017.654 2017.658 2017.662 2017.665 2017.669 2017.673
## [177] 2017.677 2017.681 2017.685 2017.688 2017.692 2017.696 2017.700 2017.704
## [185] 2017.708 2017.712 2017.715 2017.719 2017.723 2017.727 2017.731 2017.735
## [193] 2017.738 2017.742 2017.746 2017.750 2017.754 2017.758 2017.762 2017.765
## [201] 2017.769 2017.773 2017.777 2017.781 2017.785 2017.788 2017.792 2017.796
## [209] 2017.800 2017.804 2017.808 2017.812 2017.815 2017.819 2017.823 2017.827
## [217] 2017.831 2017.835 2017.838 2017.842 2017.846 2017.850 2017.854 2017.858
## [225] 2017.862 2017.865 2017.869 2017.873 2017.877 2017.881 2017.885 2017.888
## [233] 2017.892 2017.896 2017.900 2017.904 2017.908 2017.912 2017.915 2017.919
## [241] 2017.923 2017.927 2017.931 2017.935 2017.938 2017.942 2017.946 2017.950
## [249] 2017.954 2017.958 2017.962 2017.965 2017.969 2017.973 2017.977 2017.981
## [257] 2017.985 2017.988 2017.992 2017.996 2018.000 2018.004 2018.008 2018.012
## [265] 2018.015 2018.019 2018.023 2018.027 2018.031 2018.035 2018.038 2018.042
## [273] 2018.046 2018.050 2018.054 2018.058 2018.062 2018.065 2018.069 2018.073
## [281] 2018.077 2018.081 2018.085 2018.088 2018.092 2018.096 2018.100 2018.104
## [289] 2018.108 2018.112 2018.115 2018.119 2018.123 2018.127 2018.131 2018.135
## [297] 2018.138 2018.142 2018.146 2018.150 2018.154 2018.158 2018.162 2018.165
## [305] 2018.169 2018.173 2018.177 2018.181 2018.185 2018.188 2018.192 2018.196
## [313] 2018.200 2018.204 2018.208 2018.212 2018.215 2018.219 2018.223 2018.227
## [321] 2018.231 2018.235 2018.238 2018.242 2018.246 2018.250 2018.254 2018.258
## [329] 2018.262 2018.265 2018.269 2018.273 2018.277 2018.281 2018.285 2018.288
## [337] 2018.292 2018.296 2018.300 2018.304 2018.308 2018.312 2018.315 2018.319
## [345] 2018.323 2018.327 2018.331 2018.335 2018.338 2018.342 2018.346 2018.350
## [353] 2018.354 2018.358 2018.362 2018.365 2018.369 2018.373 2018.377 2018.381
## [361] 2018.385 2018.388 2018.392 2018.396 2018.400 2018.404 2018.408 2018.412
## [369] 2018.415 2018.419 2018.423 2018.427 2018.431 2018.435 2018.438 2018.442
## [377] 2018.446 2018.450 2018.454 2018.458 2018.462 2018.465 2018.469 2018.473
## [385] 2018.477 2018.481 2018.485 2018.488 2018.492 2018.496 2018.500 2018.504
## [393] 2018.508 2018.512 2018.515 2018.519 2018.523 2018.527 2018.531 2018.535
## [401] 2018.538 2018.542 2018.546 2018.550 2018.554 2018.558 2018.562 2018.565
## [409] 2018.569 2018.573 2018.577 2018.581 2018.585 2018.588 2018.592 2018.596
## [417] 2018.600 2018.604 2018.608 2018.612 2018.615 2018.619 2018.623 2018.627
## [425] 2018.631 2018.635 2018.638 2018.642 2018.646 2018.650 2018.654 2018.658
## [433] 2018.662 2018.665 2018.669 2018.673 2018.677 2018.681 2018.685 2018.688
## [441] 2018.692 2018.696 2018.700 2018.704 2018.708 2018.712 2018.715 2018.719
## [449] 2018.723 2018.727 2018.731 2018.735 2018.738 2018.742 2018.746 2018.750
## [457] 2018.754 2018.758 2018.762 2018.765 2018.769 2018.773 2018.777 2018.781
## [465] 2018.785 2018.788 2018.792 2018.796 2018.800 2018.804 2018.808 2018.812
## [473] 2018.815 2018.819 2018.823 2018.827 2018.831 2018.835 2018.838 2018.842
## [481] 2018.846 2018.850 2018.854 2018.858 2018.862 2018.865 2018.869 2018.873
## [489] 2018.877 2018.881 2018.885 2018.888 2018.892 2018.896 2018.900 2018.904
## [497] 2018.908 2018.912 2018.915 2018.919 2018.923 2018.927 2018.931 2018.935
## [505] 2018.938 2018.942 2018.946 2018.950 2018.954 2018.958 2018.962 2018.965
## [513] 2018.969 2018.973 2018.977 2018.981 2018.985 2018.988 2018.992 2018.996
## [521] 2019.000 2019.004 2019.008 2019.012 2019.015 2019.019 2019.023 2019.027
## [529] 2019.031 2019.035 2019.038 2019.042 2019.046 2019.050 2019.054 2019.058
## [537] 2019.062 2019.065 2019.069 2019.073 2019.077 2019.081 2019.085 2019.088
## [545] 2019.092 2019.096 2019.100 2019.104 2019.108 2019.112 2019.115 2019.119
## [553] 2019.123 2019.127 2019.131 2019.135 2019.138 2019.142 2019.146 2019.150
## [561] 2019.154 2019.158 2019.162 2019.165 2019.169 2019.173 2019.177 2019.181
## [569] 2019.185 2019.188 2019.192 2019.196 2019.200 2019.204 2019.208 2019.212
## [577] 2019.215 2019.219 2019.223 2019.227 2019.231 2019.235 2019.238 2019.242
## [585] 2019.246 2019.250 2019.254 2019.258 2019.262 2019.265 2019.269 2019.273
## [593] 2019.277 2019.281 2019.285 2019.288 2019.292 2019.296 2019.300 2019.304
## [601] 2019.308 2019.312 2019.315 2019.319 2019.323 2019.327 2019.331 2019.335
## [609] 2019.338 2019.342 2019.346 2019.350 2019.354 2019.358 2019.362 2019.365
## [617] 2019.369 2019.373 2019.377 2019.381 2019.385 2019.388 2019.392 2019.396
## [625] 2019.400 2019.404 2019.408 2019.412 2019.415 2019.419 2019.423 2019.427
## [633] 2019.431 2019.435 2019.438 2019.442 2019.446 2019.450 2019.454 2019.458
## [641] 2019.462 2019.465 2019.469 2019.473 2019.477 2019.481 2019.485 2019.488
## [649] 2019.492 2019.496 2019.500 2019.504 2019.508 2019.512 2019.515 2019.519
## [657] 2019.523 2019.527 2019.531 2019.535 2019.538 2019.542 2019.546 2019.550
## [665] 2019.554 2019.558 2019.562 2019.565 2019.569 2019.573 2019.577 2019.581
## [673] 2019.585 2019.588 2019.592 2019.596 2019.600 2019.604 2019.608 2019.612
## [681] 2019.615 2019.619 2019.623 2019.627 2019.631 2019.635 2019.638 2019.642
## [689] 2019.646 2019.650 2019.654 2019.658 2019.662 2019.665 2019.669 2019.673
## [697] 2019.677 2019.681 2019.685 2019.688 2019.692 2019.696 2019.700 2019.704
## [705] 2019.708 2019.712 2019.715 2019.719 2019.723 2019.727 2019.731 2019.735
## [713] 2019.738 2019.742 2019.746 2019.750 2019.754 2019.758 2019.762 2019.765
## [721] 2019.769 2019.773 2019.777 2019.781 2019.785 2019.788 2019.792 2019.796
## [729] 2019.800 2019.804 2019.808 2019.812 2019.815 2019.819 2019.823 2019.827
## [737] 2019.831 2019.835 2019.838 2019.842 2019.846 2019.850 2019.854 2019.858
## [745] 2019.862 2019.865 2019.869 2019.873 2019.877 2019.881 2019.885 2019.888
## [753] 2019.892 2019.896 2019.900 2019.904 2019.908 2019.912 2019.915 2019.919
## [761] 2019.923 2019.927 2019.931 2019.935 2019.938 2019.942 2019.946 2019.950
## [769] 2019.954 2019.958 2019.962 2019.965 2019.969 2019.973 2019.977 2019.981
## [777] 2019.985 2019.988 2019.992 2019.996 2020.000 2020.004
time(loro)[ntrain]
## [1] 2019.4
train=window(loro,end=c(2019.4))
test=window(loro,start=c(2019.4))
length(train)
## [1] 625
ntest=length(test)
ntest
## [1] 158
fcmat=matrix(0,nrow=ntest,ncol=h)
for(i in 1:ntest){
x=window(loro,end=c(2019)+(i-1)/365)
print(length(x))
refit=Arima(loro,order = c(0,1,2),include.constant = TRUE,fixed=list(0,NA,NA))
fcmat[i,]=test[i]-forecast(refit,h=h)$mean
}
## [1] 521
## [1] 521
## [1] 522
## [1] 523
## [1] 523
## [1] 524
## [1] 525
## [1] 525
## [1] 526
## [1] 527
## [1] 528
## [1] 528
## [1] 529
## [1] 530
## [1] 530
## [1] 531
## [1] 532
## [1] 533
## [1] 533
## [1] 534
## [1] 535
## [1] 535
## [1] 536
## [1] 537
## [1] 538
## [1] 538
## [1] 539
## [1] 540
## [1] 540
## [1] 541
## [1] 542
## [1] 543
## [1] 543
## [1] 544
## [1] 545
## [1] 545
## [1] 546
## [1] 547
## [1] 548
## [1] 548
## [1] 549
## [1] 550
## [1] 550
## [1] 551
## [1] 552
## [1] 553
## [1] 553
## [1] 554
## [1] 555
## [1] 555
## [1] 556
## [1] 557
## [1] 558
## [1] 558
## [1] 559
## [1] 560
## [1] 560
## [1] 561
## [1] 562
## [1] 563
## [1] 563
## [1] 564
## [1] 565
## [1] 565
## [1] 566
## [1] 567
## [1] 568
## [1] 568
## [1] 569
## [1] 570
## [1] 570
## [1] 571
## [1] 572
## [1] 573
## [1] 573
## [1] 574
## [1] 575
## [1] 575
## [1] 576
## [1] 577
## [1] 577
## [1] 578
## [1] 579
## [1] 580
## [1] 580
## [1] 581
## [1] 582
## [1] 582
## [1] 583
## [1] 584
## [1] 585
## [1] 585
## [1] 586
## [1] 587
## [1] 587
## [1] 588
## [1] 589
## [1] 590
## [1] 590
## [1] 591
## [1] 592
## [1] 592
## [1] 593
## [1] 594
## [1] 595
## [1] 595
## [1] 596
## [1] 597
## [1] 597
## [1] 598
## [1] 599
## [1] 600
## [1] 600
## [1] 601
## [1] 602
## [1] 602
## [1] 603
## [1] 604
## [1] 605
## [1] 605
## [1] 606
## [1] 607
## [1] 607
## [1] 608
## [1] 609
## [1] 610
## [1] 610
## [1] 611
## [1] 612
## [1] 612
## [1] 613
## [1] 614
## [1] 615
## [1] 615
## [1] 616
## [1] 617
## [1] 617
## [1] 618
## [1] 619
## [1] 620
## [1] 620
## [1] 621
## [1] 622
## [1] 622
## [1] 623
## [1] 624
## [1] 625
## [1] 625
## [1] 626
## [1] 627
## [1] 627
## [1] 628
## [1] 629
## [1] 629
## [1] 630
## [1] 631
## [1] 632
## [1] 632
ECMAr=mean(fcmat^2)
ECMAr
## [1] 0.004681413
h=3
lserie=length(loro)
ntrain=trunc(length(loro)*0.8)
ntrain
## [1] 625
time(loro)
## Time Series:
## Start = c(2017, 1)
## End = c(2020, 2)
## Frequency = 260
## [1] 2017.000 2017.004 2017.008 2017.012 2017.015 2017.019 2017.023 2017.027
## [9] 2017.031 2017.035 2017.038 2017.042 2017.046 2017.050 2017.054 2017.058
## [17] 2017.062 2017.065 2017.069 2017.073 2017.077 2017.081 2017.085 2017.088
## [25] 2017.092 2017.096 2017.100 2017.104 2017.108 2017.112 2017.115 2017.119
## [33] 2017.123 2017.127 2017.131 2017.135 2017.138 2017.142 2017.146 2017.150
## [41] 2017.154 2017.158 2017.162 2017.165 2017.169 2017.173 2017.177 2017.181
## [49] 2017.185 2017.188 2017.192 2017.196 2017.200 2017.204 2017.208 2017.212
## [57] 2017.215 2017.219 2017.223 2017.227 2017.231 2017.235 2017.238 2017.242
## [65] 2017.246 2017.250 2017.254 2017.258 2017.262 2017.265 2017.269 2017.273
## [73] 2017.277 2017.281 2017.285 2017.288 2017.292 2017.296 2017.300 2017.304
## [81] 2017.308 2017.312 2017.315 2017.319 2017.323 2017.327 2017.331 2017.335
## [89] 2017.338 2017.342 2017.346 2017.350 2017.354 2017.358 2017.362 2017.365
## [97] 2017.369 2017.373 2017.377 2017.381 2017.385 2017.388 2017.392 2017.396
## [105] 2017.400 2017.404 2017.408 2017.412 2017.415 2017.419 2017.423 2017.427
## [113] 2017.431 2017.435 2017.438 2017.442 2017.446 2017.450 2017.454 2017.458
## [121] 2017.462 2017.465 2017.469 2017.473 2017.477 2017.481 2017.485 2017.488
## [129] 2017.492 2017.496 2017.500 2017.504 2017.508 2017.512 2017.515 2017.519
## [137] 2017.523 2017.527 2017.531 2017.535 2017.538 2017.542 2017.546 2017.550
## [145] 2017.554 2017.558 2017.562 2017.565 2017.569 2017.573 2017.577 2017.581
## [153] 2017.585 2017.588 2017.592 2017.596 2017.600 2017.604 2017.608 2017.612
## [161] 2017.615 2017.619 2017.623 2017.627 2017.631 2017.635 2017.638 2017.642
## [169] 2017.646 2017.650 2017.654 2017.658 2017.662 2017.665 2017.669 2017.673
## [177] 2017.677 2017.681 2017.685 2017.688 2017.692 2017.696 2017.700 2017.704
## [185] 2017.708 2017.712 2017.715 2017.719 2017.723 2017.727 2017.731 2017.735
## [193] 2017.738 2017.742 2017.746 2017.750 2017.754 2017.758 2017.762 2017.765
## [201] 2017.769 2017.773 2017.777 2017.781 2017.785 2017.788 2017.792 2017.796
## [209] 2017.800 2017.804 2017.808 2017.812 2017.815 2017.819 2017.823 2017.827
## [217] 2017.831 2017.835 2017.838 2017.842 2017.846 2017.850 2017.854 2017.858
## [225] 2017.862 2017.865 2017.869 2017.873 2017.877 2017.881 2017.885 2017.888
## [233] 2017.892 2017.896 2017.900 2017.904 2017.908 2017.912 2017.915 2017.919
## [241] 2017.923 2017.927 2017.931 2017.935 2017.938 2017.942 2017.946 2017.950
## [249] 2017.954 2017.958 2017.962 2017.965 2017.969 2017.973 2017.977 2017.981
## [257] 2017.985 2017.988 2017.992 2017.996 2018.000 2018.004 2018.008 2018.012
## [265] 2018.015 2018.019 2018.023 2018.027 2018.031 2018.035 2018.038 2018.042
## [273] 2018.046 2018.050 2018.054 2018.058 2018.062 2018.065 2018.069 2018.073
## [281] 2018.077 2018.081 2018.085 2018.088 2018.092 2018.096 2018.100 2018.104
## [289] 2018.108 2018.112 2018.115 2018.119 2018.123 2018.127 2018.131 2018.135
## [297] 2018.138 2018.142 2018.146 2018.150 2018.154 2018.158 2018.162 2018.165
## [305] 2018.169 2018.173 2018.177 2018.181 2018.185 2018.188 2018.192 2018.196
## [313] 2018.200 2018.204 2018.208 2018.212 2018.215 2018.219 2018.223 2018.227
## [321] 2018.231 2018.235 2018.238 2018.242 2018.246 2018.250 2018.254 2018.258
## [329] 2018.262 2018.265 2018.269 2018.273 2018.277 2018.281 2018.285 2018.288
## [337] 2018.292 2018.296 2018.300 2018.304 2018.308 2018.312 2018.315 2018.319
## [345] 2018.323 2018.327 2018.331 2018.335 2018.338 2018.342 2018.346 2018.350
## [353] 2018.354 2018.358 2018.362 2018.365 2018.369 2018.373 2018.377 2018.381
## [361] 2018.385 2018.388 2018.392 2018.396 2018.400 2018.404 2018.408 2018.412
## [369] 2018.415 2018.419 2018.423 2018.427 2018.431 2018.435 2018.438 2018.442
## [377] 2018.446 2018.450 2018.454 2018.458 2018.462 2018.465 2018.469 2018.473
## [385] 2018.477 2018.481 2018.485 2018.488 2018.492 2018.496 2018.500 2018.504
## [393] 2018.508 2018.512 2018.515 2018.519 2018.523 2018.527 2018.531 2018.535
## [401] 2018.538 2018.542 2018.546 2018.550 2018.554 2018.558 2018.562 2018.565
## [409] 2018.569 2018.573 2018.577 2018.581 2018.585 2018.588 2018.592 2018.596
## [417] 2018.600 2018.604 2018.608 2018.612 2018.615 2018.619 2018.623 2018.627
## [425] 2018.631 2018.635 2018.638 2018.642 2018.646 2018.650 2018.654 2018.658
## [433] 2018.662 2018.665 2018.669 2018.673 2018.677 2018.681 2018.685 2018.688
## [441] 2018.692 2018.696 2018.700 2018.704 2018.708 2018.712 2018.715 2018.719
## [449] 2018.723 2018.727 2018.731 2018.735 2018.738 2018.742 2018.746 2018.750
## [457] 2018.754 2018.758 2018.762 2018.765 2018.769 2018.773 2018.777 2018.781
## [465] 2018.785 2018.788 2018.792 2018.796 2018.800 2018.804 2018.808 2018.812
## [473] 2018.815 2018.819 2018.823 2018.827 2018.831 2018.835 2018.838 2018.842
## [481] 2018.846 2018.850 2018.854 2018.858 2018.862 2018.865 2018.869 2018.873
## [489] 2018.877 2018.881 2018.885 2018.888 2018.892 2018.896 2018.900 2018.904
## [497] 2018.908 2018.912 2018.915 2018.919 2018.923 2018.927 2018.931 2018.935
## [505] 2018.938 2018.942 2018.946 2018.950 2018.954 2018.958 2018.962 2018.965
## [513] 2018.969 2018.973 2018.977 2018.981 2018.985 2018.988 2018.992 2018.996
## [521] 2019.000 2019.004 2019.008 2019.012 2019.015 2019.019 2019.023 2019.027
## [529] 2019.031 2019.035 2019.038 2019.042 2019.046 2019.050 2019.054 2019.058
## [537] 2019.062 2019.065 2019.069 2019.073 2019.077 2019.081 2019.085 2019.088
## [545] 2019.092 2019.096 2019.100 2019.104 2019.108 2019.112 2019.115 2019.119
## [553] 2019.123 2019.127 2019.131 2019.135 2019.138 2019.142 2019.146 2019.150
## [561] 2019.154 2019.158 2019.162 2019.165 2019.169 2019.173 2019.177 2019.181
## [569] 2019.185 2019.188 2019.192 2019.196 2019.200 2019.204 2019.208 2019.212
## [577] 2019.215 2019.219 2019.223 2019.227 2019.231 2019.235 2019.238 2019.242
## [585] 2019.246 2019.250 2019.254 2019.258 2019.262 2019.265 2019.269 2019.273
## [593] 2019.277 2019.281 2019.285 2019.288 2019.292 2019.296 2019.300 2019.304
## [601] 2019.308 2019.312 2019.315 2019.319 2019.323 2019.327 2019.331 2019.335
## [609] 2019.338 2019.342 2019.346 2019.350 2019.354 2019.358 2019.362 2019.365
## [617] 2019.369 2019.373 2019.377 2019.381 2019.385 2019.388 2019.392 2019.396
## [625] 2019.400 2019.404 2019.408 2019.412 2019.415 2019.419 2019.423 2019.427
## [633] 2019.431 2019.435 2019.438 2019.442 2019.446 2019.450 2019.454 2019.458
## [641] 2019.462 2019.465 2019.469 2019.473 2019.477 2019.481 2019.485 2019.488
## [649] 2019.492 2019.496 2019.500 2019.504 2019.508 2019.512 2019.515 2019.519
## [657] 2019.523 2019.527 2019.531 2019.535 2019.538 2019.542 2019.546 2019.550
## [665] 2019.554 2019.558 2019.562 2019.565 2019.569 2019.573 2019.577 2019.581
## [673] 2019.585 2019.588 2019.592 2019.596 2019.600 2019.604 2019.608 2019.612
## [681] 2019.615 2019.619 2019.623 2019.627 2019.631 2019.635 2019.638 2019.642
## [689] 2019.646 2019.650 2019.654 2019.658 2019.662 2019.665 2019.669 2019.673
## [697] 2019.677 2019.681 2019.685 2019.688 2019.692 2019.696 2019.700 2019.704
## [705] 2019.708 2019.712 2019.715 2019.719 2019.723 2019.727 2019.731 2019.735
## [713] 2019.738 2019.742 2019.746 2019.750 2019.754 2019.758 2019.762 2019.765
## [721] 2019.769 2019.773 2019.777 2019.781 2019.785 2019.788 2019.792 2019.796
## [729] 2019.800 2019.804 2019.808 2019.812 2019.815 2019.819 2019.823 2019.827
## [737] 2019.831 2019.835 2019.838 2019.842 2019.846 2019.850 2019.854 2019.858
## [745] 2019.862 2019.865 2019.869 2019.873 2019.877 2019.881 2019.885 2019.888
## [753] 2019.892 2019.896 2019.900 2019.904 2019.908 2019.912 2019.915 2019.919
## [761] 2019.923 2019.927 2019.931 2019.935 2019.938 2019.942 2019.946 2019.950
## [769] 2019.954 2019.958 2019.962 2019.965 2019.969 2019.973 2019.977 2019.981
## [777] 2019.985 2019.988 2019.992 2019.996 2020.000 2020.004
time(loro)[ntrain]###Me entrega la ultima fecha de la posici?n ntrain
## [1] 2019.4
train=window(loro,end=c(2019.4))
test=window(loro,start=c(2019.4))
length(train)
## [1] 625
ntest=length(test)
ntest
## [1] 158
fcmat=matrix(0,nrow=ntest,ncol=h)
for(i in 1:ntest){
x=window(loro,end=c(2019)+(i-1)/365)
print(length(x))
refit=Arima(loro,order = c(2,1,0),include.constant = TRUE,fixed=list(0,NA,NA))
fcmat[i,]=test[i]-forecast(refit,h=h)$mean
}
## [1] 521
## [1] 521
## [1] 522
## [1] 523
## [1] 523
## [1] 524
## [1] 525
## [1] 525
## [1] 526
## [1] 527
## [1] 528
## [1] 528
## [1] 529
## [1] 530
## [1] 530
## [1] 531
## [1] 532
## [1] 533
## [1] 533
## [1] 534
## [1] 535
## [1] 535
## [1] 536
## [1] 537
## [1] 538
## [1] 538
## [1] 539
## [1] 540
## [1] 540
## [1] 541
## [1] 542
## [1] 543
## [1] 543
## [1] 544
## [1] 545
## [1] 545
## [1] 546
## [1] 547
## [1] 548
## [1] 548
## [1] 549
## [1] 550
## [1] 550
## [1] 551
## [1] 552
## [1] 553
## [1] 553
## [1] 554
## [1] 555
## [1] 555
## [1] 556
## [1] 557
## [1] 558
## [1] 558
## [1] 559
## [1] 560
## [1] 560
## [1] 561
## [1] 562
## [1] 563
## [1] 563
## [1] 564
## [1] 565
## [1] 565
## [1] 566
## [1] 567
## [1] 568
## [1] 568
## [1] 569
## [1] 570
## [1] 570
## [1] 571
## [1] 572
## [1] 573
## [1] 573
## [1] 574
## [1] 575
## [1] 575
## [1] 576
## [1] 577
## [1] 577
## [1] 578
## [1] 579
## [1] 580
## [1] 580
## [1] 581
## [1] 582
## [1] 582
## [1] 583
## [1] 584
## [1] 585
## [1] 585
## [1] 586
## [1] 587
## [1] 587
## [1] 588
## [1] 589
## [1] 590
## [1] 590
## [1] 591
## [1] 592
## [1] 592
## [1] 593
## [1] 594
## [1] 595
## [1] 595
## [1] 596
## [1] 597
## [1] 597
## [1] 598
## [1] 599
## [1] 600
## [1] 600
## [1] 601
## [1] 602
## [1] 602
## [1] 603
## [1] 604
## [1] 605
## [1] 605
## [1] 606
## [1] 607
## [1] 607
## [1] 608
## [1] 609
## [1] 610
## [1] 610
## [1] 611
## [1] 612
## [1] 612
## [1] 613
## [1] 614
## [1] 615
## [1] 615
## [1] 616
## [1] 617
## [1] 617
## [1] 618
## [1] 619
## [1] 620
## [1] 620
## [1] 621
## [1] 622
## [1] 622
## [1] 623
## [1] 624
## [1] 625
## [1] 625
## [1] 626
## [1] 627
## [1] 627
## [1] 628
## [1] 629
## [1] 629
## [1] 630
## [1] 631
## [1] 632
## [1] 632
ECMMa=mean(fcmat^2)
ECMMa
## [1] 0.0046877
Ajustamos un STL
h=3
lserie=length(loro)
ntrain=trunc(length(loro)*0.80)
ntrain
## [1] 625
time(loro)
## Time Series:
## Start = c(2017, 1)
## End = c(2020, 2)
## Frequency = 260
## [1] 2017.000 2017.004 2017.008 2017.012 2017.015 2017.019 2017.023 2017.027
## [9] 2017.031 2017.035 2017.038 2017.042 2017.046 2017.050 2017.054 2017.058
## [17] 2017.062 2017.065 2017.069 2017.073 2017.077 2017.081 2017.085 2017.088
## [25] 2017.092 2017.096 2017.100 2017.104 2017.108 2017.112 2017.115 2017.119
## [33] 2017.123 2017.127 2017.131 2017.135 2017.138 2017.142 2017.146 2017.150
## [41] 2017.154 2017.158 2017.162 2017.165 2017.169 2017.173 2017.177 2017.181
## [49] 2017.185 2017.188 2017.192 2017.196 2017.200 2017.204 2017.208 2017.212
## [57] 2017.215 2017.219 2017.223 2017.227 2017.231 2017.235 2017.238 2017.242
## [65] 2017.246 2017.250 2017.254 2017.258 2017.262 2017.265 2017.269 2017.273
## [73] 2017.277 2017.281 2017.285 2017.288 2017.292 2017.296 2017.300 2017.304
## [81] 2017.308 2017.312 2017.315 2017.319 2017.323 2017.327 2017.331 2017.335
## [89] 2017.338 2017.342 2017.346 2017.350 2017.354 2017.358 2017.362 2017.365
## [97] 2017.369 2017.373 2017.377 2017.381 2017.385 2017.388 2017.392 2017.396
## [105] 2017.400 2017.404 2017.408 2017.412 2017.415 2017.419 2017.423 2017.427
## [113] 2017.431 2017.435 2017.438 2017.442 2017.446 2017.450 2017.454 2017.458
## [121] 2017.462 2017.465 2017.469 2017.473 2017.477 2017.481 2017.485 2017.488
## [129] 2017.492 2017.496 2017.500 2017.504 2017.508 2017.512 2017.515 2017.519
## [137] 2017.523 2017.527 2017.531 2017.535 2017.538 2017.542 2017.546 2017.550
## [145] 2017.554 2017.558 2017.562 2017.565 2017.569 2017.573 2017.577 2017.581
## [153] 2017.585 2017.588 2017.592 2017.596 2017.600 2017.604 2017.608 2017.612
## [161] 2017.615 2017.619 2017.623 2017.627 2017.631 2017.635 2017.638 2017.642
## [169] 2017.646 2017.650 2017.654 2017.658 2017.662 2017.665 2017.669 2017.673
## [177] 2017.677 2017.681 2017.685 2017.688 2017.692 2017.696 2017.700 2017.704
## [185] 2017.708 2017.712 2017.715 2017.719 2017.723 2017.727 2017.731 2017.735
## [193] 2017.738 2017.742 2017.746 2017.750 2017.754 2017.758 2017.762 2017.765
## [201] 2017.769 2017.773 2017.777 2017.781 2017.785 2017.788 2017.792 2017.796
## [209] 2017.800 2017.804 2017.808 2017.812 2017.815 2017.819 2017.823 2017.827
## [217] 2017.831 2017.835 2017.838 2017.842 2017.846 2017.850 2017.854 2017.858
## [225] 2017.862 2017.865 2017.869 2017.873 2017.877 2017.881 2017.885 2017.888
## [233] 2017.892 2017.896 2017.900 2017.904 2017.908 2017.912 2017.915 2017.919
## [241] 2017.923 2017.927 2017.931 2017.935 2017.938 2017.942 2017.946 2017.950
## [249] 2017.954 2017.958 2017.962 2017.965 2017.969 2017.973 2017.977 2017.981
## [257] 2017.985 2017.988 2017.992 2017.996 2018.000 2018.004 2018.008 2018.012
## [265] 2018.015 2018.019 2018.023 2018.027 2018.031 2018.035 2018.038 2018.042
## [273] 2018.046 2018.050 2018.054 2018.058 2018.062 2018.065 2018.069 2018.073
## [281] 2018.077 2018.081 2018.085 2018.088 2018.092 2018.096 2018.100 2018.104
## [289] 2018.108 2018.112 2018.115 2018.119 2018.123 2018.127 2018.131 2018.135
## [297] 2018.138 2018.142 2018.146 2018.150 2018.154 2018.158 2018.162 2018.165
## [305] 2018.169 2018.173 2018.177 2018.181 2018.185 2018.188 2018.192 2018.196
## [313] 2018.200 2018.204 2018.208 2018.212 2018.215 2018.219 2018.223 2018.227
## [321] 2018.231 2018.235 2018.238 2018.242 2018.246 2018.250 2018.254 2018.258
## [329] 2018.262 2018.265 2018.269 2018.273 2018.277 2018.281 2018.285 2018.288
## [337] 2018.292 2018.296 2018.300 2018.304 2018.308 2018.312 2018.315 2018.319
## [345] 2018.323 2018.327 2018.331 2018.335 2018.338 2018.342 2018.346 2018.350
## [353] 2018.354 2018.358 2018.362 2018.365 2018.369 2018.373 2018.377 2018.381
## [361] 2018.385 2018.388 2018.392 2018.396 2018.400 2018.404 2018.408 2018.412
## [369] 2018.415 2018.419 2018.423 2018.427 2018.431 2018.435 2018.438 2018.442
## [377] 2018.446 2018.450 2018.454 2018.458 2018.462 2018.465 2018.469 2018.473
## [385] 2018.477 2018.481 2018.485 2018.488 2018.492 2018.496 2018.500 2018.504
## [393] 2018.508 2018.512 2018.515 2018.519 2018.523 2018.527 2018.531 2018.535
## [401] 2018.538 2018.542 2018.546 2018.550 2018.554 2018.558 2018.562 2018.565
## [409] 2018.569 2018.573 2018.577 2018.581 2018.585 2018.588 2018.592 2018.596
## [417] 2018.600 2018.604 2018.608 2018.612 2018.615 2018.619 2018.623 2018.627
## [425] 2018.631 2018.635 2018.638 2018.642 2018.646 2018.650 2018.654 2018.658
## [433] 2018.662 2018.665 2018.669 2018.673 2018.677 2018.681 2018.685 2018.688
## [441] 2018.692 2018.696 2018.700 2018.704 2018.708 2018.712 2018.715 2018.719
## [449] 2018.723 2018.727 2018.731 2018.735 2018.738 2018.742 2018.746 2018.750
## [457] 2018.754 2018.758 2018.762 2018.765 2018.769 2018.773 2018.777 2018.781
## [465] 2018.785 2018.788 2018.792 2018.796 2018.800 2018.804 2018.808 2018.812
## [473] 2018.815 2018.819 2018.823 2018.827 2018.831 2018.835 2018.838 2018.842
## [481] 2018.846 2018.850 2018.854 2018.858 2018.862 2018.865 2018.869 2018.873
## [489] 2018.877 2018.881 2018.885 2018.888 2018.892 2018.896 2018.900 2018.904
## [497] 2018.908 2018.912 2018.915 2018.919 2018.923 2018.927 2018.931 2018.935
## [505] 2018.938 2018.942 2018.946 2018.950 2018.954 2018.958 2018.962 2018.965
## [513] 2018.969 2018.973 2018.977 2018.981 2018.985 2018.988 2018.992 2018.996
## [521] 2019.000 2019.004 2019.008 2019.012 2019.015 2019.019 2019.023 2019.027
## [529] 2019.031 2019.035 2019.038 2019.042 2019.046 2019.050 2019.054 2019.058
## [537] 2019.062 2019.065 2019.069 2019.073 2019.077 2019.081 2019.085 2019.088
## [545] 2019.092 2019.096 2019.100 2019.104 2019.108 2019.112 2019.115 2019.119
## [553] 2019.123 2019.127 2019.131 2019.135 2019.138 2019.142 2019.146 2019.150
## [561] 2019.154 2019.158 2019.162 2019.165 2019.169 2019.173 2019.177 2019.181
## [569] 2019.185 2019.188 2019.192 2019.196 2019.200 2019.204 2019.208 2019.212
## [577] 2019.215 2019.219 2019.223 2019.227 2019.231 2019.235 2019.238 2019.242
## [585] 2019.246 2019.250 2019.254 2019.258 2019.262 2019.265 2019.269 2019.273
## [593] 2019.277 2019.281 2019.285 2019.288 2019.292 2019.296 2019.300 2019.304
## [601] 2019.308 2019.312 2019.315 2019.319 2019.323 2019.327 2019.331 2019.335
## [609] 2019.338 2019.342 2019.346 2019.350 2019.354 2019.358 2019.362 2019.365
## [617] 2019.369 2019.373 2019.377 2019.381 2019.385 2019.388 2019.392 2019.396
## [625] 2019.400 2019.404 2019.408 2019.412 2019.415 2019.419 2019.423 2019.427
## [633] 2019.431 2019.435 2019.438 2019.442 2019.446 2019.450 2019.454 2019.458
## [641] 2019.462 2019.465 2019.469 2019.473 2019.477 2019.481 2019.485 2019.488
## [649] 2019.492 2019.496 2019.500 2019.504 2019.508 2019.512 2019.515 2019.519
## [657] 2019.523 2019.527 2019.531 2019.535 2019.538 2019.542 2019.546 2019.550
## [665] 2019.554 2019.558 2019.562 2019.565 2019.569 2019.573 2019.577 2019.581
## [673] 2019.585 2019.588 2019.592 2019.596 2019.600 2019.604 2019.608 2019.612
## [681] 2019.615 2019.619 2019.623 2019.627 2019.631 2019.635 2019.638 2019.642
## [689] 2019.646 2019.650 2019.654 2019.658 2019.662 2019.665 2019.669 2019.673
## [697] 2019.677 2019.681 2019.685 2019.688 2019.692 2019.696 2019.700 2019.704
## [705] 2019.708 2019.712 2019.715 2019.719 2019.723 2019.727 2019.731 2019.735
## [713] 2019.738 2019.742 2019.746 2019.750 2019.754 2019.758 2019.762 2019.765
## [721] 2019.769 2019.773 2019.777 2019.781 2019.785 2019.788 2019.792 2019.796
## [729] 2019.800 2019.804 2019.808 2019.812 2019.815 2019.819 2019.823 2019.827
## [737] 2019.831 2019.835 2019.838 2019.842 2019.846 2019.850 2019.854 2019.858
## [745] 2019.862 2019.865 2019.869 2019.873 2019.877 2019.881 2019.885 2019.888
## [753] 2019.892 2019.896 2019.900 2019.904 2019.908 2019.912 2019.915 2019.919
## [761] 2019.923 2019.927 2019.931 2019.935 2019.938 2019.942 2019.946 2019.950
## [769] 2019.954 2019.958 2019.962 2019.965 2019.969 2019.973 2019.977 2019.981
## [777] 2019.985 2019.988 2019.992 2019.996 2020.000 2020.004
time(loro)[ntrain]
## [1] 2019.4
train=window(loro,end=time(loro)[ntrain])
test=window(loro,start=time(loro)[ntrain]+1/365)
length(train)
## [1] 625
ntest=length(test)
ntest
## [1] 157
lserie
## [1] 782
fchstepahe=matrix(0,nrow=ntest,ncol=h)
verval=cbind(test[1:ntest])
for(j in 2:h){
verval=cbind(verval,c(test[j:ntest],rep(NA,j-1)))
}
verval=cbind(test[1:ntest],c(test[2:ntest],NA),c(test[3:ntest],NA,NA))
STLPM25_train=stats::stl(train,s.window="periodic")
for(i in 1:(ntest)){
x=window(loro,end=time(loro)[ntrain]+(i-1)/365)
#print(length(x))
refit=stats::stl(x,s.window="periodic")
fchstepahe[i,]=as.numeric(forecast::forecast(refit,h=h)$mean)
}
errores_pred=verval -fchstepahe
ECMLst=apply(errores_pred^2,MARGIN = 2,mean,na.rm=TRUE)
ECMLst
## [1] 0.003992625 0.004224188 0.004469548
ECMAr
## [1] 0.004681413
ECMMa
## [1] 0.0046877
ECMLst
## [1] 0.003992625 0.004224188 0.004469548
Analisis de los residuales para el modelo elegido:
residuales=ajusteARIMA$residuals
acf(residuales)
pacf(residuales)
Box.test(residuales, lag =365 , type = "Ljung-Box", fitdf = 12)
##
## Box-Ljung test
##
## data: residuales
## X-squared = 275.46, df = 353, p-value = 0.9992
res=residuales
cum=cumsum(res)/sd(res)
N=length(res)
cumq=cumsum(res^2)/sum(res^2)
Af=0.948 ###Cuantil del 95% para la estad?stica cusum
co=0.09234
LS=Af*sqrt(N)+2*Af*c(1:length(res))/sqrt(N)
LI=-LS
LQS=co+(1:length(res))/N
LQI=-co+(1:length(res))/N
plot(cum,type="l",ylim=c(min(LI),max(LS)),xlab="t",ylab="",main="CUSUM")
lines(LS,type="S",col="red")
lines(LI,type="S",col="red")
#CUSUMSQ
plot(cumq,type="l",xlab="t",ylab="",main="CUSUMSQ")
lines(LQS,type="S",col="red")
lines(LQI,type="S",col="red")
Luego, con ello, nuestro pronóstico está dado por:
pronosticos12=forecast::forecast(ajusteARIMA,h=12,level=0.95)
plot(pronosticos12)